<template>
	<view class="enterprise_add_layout">
		<uni-forms ref="form" :model="data" :rules="rules" err-show-type="toast">
			<uni-forms-item label="企业名称:" label-width="92px" label-align="right" required name="name">
				<uni-easyinput placeholder="请输入企业名称" :maxlength='-1' trim v-model="data.name"
					class="uni-easyinput"></uni-easyinput>
			</uni-forms-item>

			<uni-forms-item label="企业Logo:" label-width="92px" label-align="right" name="logo">
				<uni-file-picker class="uni_file_picker" limit="1" v-model="data.logo"></uni-file-picker>
			</uni-forms-item>

			<uni-forms-item label="法定代表人:" label-width="92px" label-align="right" required
				name="corporate_legal_person">
				<uni-easyinput placeholder="请输入法定代表人" :maxlength='-1' trim v-model="data.corporate_legal_person"
					class="uni-easyinput"></uni-easyinput>
			</uni-forms-item>

			<uni-forms-item label="联系人:" label-width="92px" label-align="right" name="contacts">
				<uni-easyinput placeholder="请输入联系人" :maxlength='-1' trim v-model="data.contacts"
					class="uni-easyinput"></uni-easyinput>
			</uni-forms-item>

			<uni-forms-item label="电话:" label-width="92px" label-align="right" name="telephone">
				<uni-easyinput placeholder="请输入电话" type="number" :maxlength='-1' trim v-model="data.telephone"
					class="uni-easyinput"></uni-easyinput>
			</uni-forms-item>


			<uni-forms-item label="注册地址:" label-width="92px" label-align="right" name="address">
				<uni-easyinput type='textarea' autoHeight placeholder="请输入注册地址" :maxlength='-1' trim
					v-model="data.address" class="uni-easyinput"></uni-easyinput>
			</uni-forms-item>

			<uni-forms-item label="经营状态:" label-width="92px" label-align="right" name="status">
				<uni-data-checkbox :localdata="enterprise_status_arrs" v-model="data.status"
					@change="change"></uni-data-checkbox>
			</uni-forms-item>

			<view class="uni-button-group">
				<button type="primary" class="uni-button" @click="submit">提交</button>
			</view>
		</uni-forms>
	</view>
</template>

<script setup>
	import {
		ref
	} from 'vue';

	import {
		onLoad
	} from '@dcloudio/uni-app'

	import {
		enterprise_status_arrs
	} from '@/utils/enterprise.js'

	const form = ref(null)

	const data = ref({
		_id: '',
		name: '',
		logo: [],
		corporate_legal_person: '',
		address: '',
		contacts: '',
		telephone: '',
		status: 0,
	})

	// 规则
	const rules = ref({
		name: {
			rules: [{
				required: true,
				errorMessage: '请输入企业名称',
			}]
		},
		corporate_legal_person: {
			rules: [{
				required: true,
				errorMessage: '请输入法定代表人',
			}]
		}
	})

	const enterprise = uniCloud.importObject('opendb-enterprise')

	onLoad(async (params) => {
		if (!params.id) {
			return
		}
		let res = await enterprise.opendb_enterprise_detail(params.id)
		console.log('企业信息', res);
		if (res.errCode != 0) {
			return
		}
		data.value = res.data
	})

	/**
	 * 验证表单并提交
	 */
	function submit() {
		uni.showLoading({
			mask: true
		})
		form.value.validate().then((res) => {
			uni.hideLoading()
			return submitForm(res)
		}).catch(() => {}).finally(() => {
			uni.hideLoading()
		})
	}

	/**
	 * 提交表单
	 */
	async function submitForm(value) {
		let res = await enterprise.opendb_enterprise_add(data.value)
		if (res.errCode != 0) {
			uni.showModal({
				content: res.errMsg || '请求服务失败',
				showCancel: false
			})
			return
		}
		uni.showToast({
			icon: 'none',
			title: data.value._id.length > 0 ? '修改成功' : '新增成功'
		})
		uni.$emit('enterprise_change')
		uni.navigateBack()
	}

	function change(e) {
		console.log(data.value);
	}
</script>

<style lang="scss" scoped>
	.enterprise_add_layout {
		padding: 20rpx 40rpx;

		.uni-easyinput {
			font-size: 40rpx;
		}

		.uni_file_picker {}

		.uni-button {
			width: 184px;
		}
	}
</style>